package ihis.core.report;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ihis.core.db.IHISDatabase;
import ihis.model.GeneralFilter;
import ihis.model.Report;
import ihis.model.Schedule;
import ihis.model.ScheduleReport;

public class PersonalScheduleReportFactory extends ReportFactory {

	/* (non-Javadoc)
	 * @see ihis.core.report.ReportFactory#getReport(ihis.model.GeneralFilter)
	 */
	@Override
	public Report getReport(GeneralFilter filter) {
		ScheduleReport report = new ScheduleReport();
		
	    try {
			@SuppressWarnings("unchecked")
			List<Schedule> scheduleList = (ArrayList<Schedule>) IHISDatabase.getSqlMapClient().queryForList("SelectList.Schedule", filter);
			
			if(scheduleList == null){
				return report;
			}
			
			report.setScheduleList(scheduleList);
			
			Schedule summary = new Schedule();
			/* 진행 개수 */
			int inProgressCnt = 0;
			/* 완료 개수 */
			int completedCnt = 0;
			/* 지연 개수 */
			int delayedCnt = 0;
			/* 차주 진행 예정 개수 */
			int plannedCnt = 0;
			
			for(int i=0; scheduleList != null && i < scheduleList.size(); i++){
				Schedule tempSchedule = scheduleList.get(i);
				inProgressCnt += tempSchedule.getInProgressCnt();
				completedCnt += tempSchedule.getCompletedCnt();
				delayedCnt += tempSchedule.getDelayedCnt();
				plannedCnt += tempSchedule.getPlannedCnt();
			}
			
			summary.setCompletedCnt(completedCnt);
			summary.setDelayedCnt(delayedCnt);
			summary.setInProgressCnt(inProgressCnt);
			summary.setPlannedCnt(plannedCnt);
			
			report.setSummary(summary);
			
			report.setSearchCondition(filter);
			
	    } catch (SQLException e) {
			e.printStackTrace();
		}
		
		return report;
	}

}
